<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Role;
use App\Models\Permission;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Role $role)
    {
        $roles = $role->filter($request->all())->paginate(config('api.page.limit'));
        $name = $request->name;
        return view('admins.role.index',compact('roles','name'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admins.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Role $role)
    {
        $result = $role::create($request->all());
        if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.roles.index')->with('success', '添加成功');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        return view('admins.role.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Role $role)
    {
        $result = $role->update($request->all());
        if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.roles.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Role $role)
    {
        dd($role);
    }

    /**
     * 分配权限
     */
    
    public function setPermissions(Role $role, Permission $permission)
    {

    	//dd($role->permissions->toArray());
    	return view('admins.role.setpermissions', compact('role'));
    }

    public function getPermissions(Request $request, Role $role, Permission $permission)
    {
    	$role_id = $request->role_id;
    	$role = $role->with('permissions')->find($role_id);
    	$role_permissions = optional($role)->permissions;
    	$role_permissions = $role_permissions->pluck('id');
    	$permission = $permission->select('id','name','title','guard_name','parent_id')->get();
        $newPermissions = collect([]);
        $parentP = $permission->where('parent_id',0);
        foreach($parentP as $key => $value){
            $newPermissions->push($value);
            $children = $permission->where('parent_id',$value->id);
            if($children->isNotEmpty()){
                foreach($children as $cKey => $cValue){
                    $newPermissions->push($cValue);
                }
            }
        }
    	return json_encode(['code' => 200, 'msg' => 'success', 'data' => compact('role_permissions','newPermissions')]);
    }

    
    public function setPermissionStore(Request $request, Role $role, Permission $permission)
    {	
    	$role_id = $request->role_id;
    	$role = $role->with('permissions')->find($role_id);
        $permissions = $request->permissions;
        $res = optional($role)->syncPermissions(explode(',', $permissions));
        if(!$res){
            return redirect()->route('admin.roles.index')->with('warning','编辑失败'); 
        }         	
    	return redirect()->route('admin.roles.index')->with('success', '编辑成功');
    }
}
